package app.system.controll;

import app.system.entity.MusicMusicLisit;
import app.system.entity.Page;
import app.system.entity.SysMusicList;
import app.system.entity.SysUser;
import app.system.service.SysMusicListService;
import app.utils.Result.AjaxResult;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SysMusicListControll {
    @Autowired
    private SysMusicListService musicListService;

    @ResponseBody
    @GetMapping("getMusicListByUser")
    public AjaxResult getMusicListByUser(@RequestParam(value = "pageNum", defaultValue = "1" ,required = false) int pageNum, @RequestParam(value = "pageSize", defaultValue = "8",required = false) int pageSize){
        Page page = new Page();
        page.setPageSize(pageSize);
        page.setPageNum(pageNum);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysMusicList musicList = new SysMusicList();
        musicList.setList_creatorid(sysUser.getUser_id());
        PageInfo<SysMusicList> sysMusicLists = musicListService.selectMusicListByTage(musicList,page);
        if (sysMusicLists.getList().size()<1){
            return AjaxResult.success("您现在还没有歌单哦");
        }else {
            return AjaxResult.success(sysMusicLists);
        }
    }

    @PostMapping("deleteMusicList")
    public AjaxResult deleteMusicList(@RequestBody SysMusicList sysMusicList){
        int de = musicListService.deleteSysMusicList(sysMusicList.getList_id());
        if (de<0){
            return AjaxResult.error("删除失败");
        }else {
            return AjaxResult.success("删除成功");
        }
    }

    @PostMapping("getMusicByType")
    public AjaxResult getMusicByType(@RequestBody SysMusicList sysMusicList){
        SysMusicList sysMusicList1 = new SysMusicList();
        sysMusicList1.setList_type(sysMusicList.getList_type());
        Page page = new Page();
        page.setPageNum(1);
        PageInfo<SysMusicList> sysMusicListList = musicListService.selectMusicListByTage(sysMusicList1,page);
        System.out.println(JSONObject.toJSONString(sysMusicListList));
        return AjaxResult.success(sysMusicListList);
    }

    @PostMapping("searchMusicList")
    public AjaxResult searchMusicList(@RequestBody SysMusicList sysMusicList){
        return AjaxResult.success(musicListService.selectMusicListByLike(sysMusicList.getList_name()));
    }
}
